package com.dnjn.happiness.system.util;

import cn.hutool.core.util.ObjectUtil;
import com.dnjn.happiness.system.domain.vo.ExportTeamVO;
import org.springframework.util.ObjectUtils;
import org.xmind.core.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 树形结构数据导出excel工具
 * <p>
 */
@SuppressWarnings("ALL")
public class TreeXmindlUtil<T extends ExportTeamVO> {

    public static List<ExportTeamVO> buildTreeList(List<ExportTeamVO> list) {
        List<ExportTeamVO> returnList = new ArrayList<>();
        List<Long> longList = list.stream().map(ExportTeamVO::getTeamId).collect(Collectors.toList());
        for (ExportTeamVO item : list) {
            if (!longList.contains(item.getParentId())){
                //不包含为顶级得
                //计算当前顶级节点的所有子节点
                funRecursion(list,item);
                returnList.add(item);
            }
        }
        return returnList;
    }

    private static void funRecursion(List<ExportTeamVO> list, ExportTeamVO item) {
        //查询下级节点列表
        List<ExportTeamVO> childList = getChildList(list,item);
        item.setChild(childList);
        //递归遍历下级节点是否还有下级，组装下级数据
        for (ExportTeamVO child : childList) {
            if (getChildList(list,child).size() > 0){
                funRecursion(list,child);
            }
        }
    }

    private static List<ExportTeamVO> getChildList(List<ExportTeamVO> list, ExportTeamVO item) {
        List<ExportTeamVO> childList = new ArrayList<>();
        Iterator<ExportTeamVO> iterator = list.iterator();
        while (iterator.hasNext()){
            ExportTeamVO nextTeam = iterator.next();
            if (!ObjectUtils.isEmpty(nextTeam.getParentId()) && nextTeam.getParentId().equals(item.getTeamId())){
                childList.add(nextTeam);
            }
        }
        return childList;
    }

    /*public static void exportMethod(List<ExportTeamVO> list) {
        //创建思维导图的工作空间
        IWorkbookBuilder workbookBuilder = Core.getWorkbookBuilder();
        IWorkbook workbook = workbookBuilder.createWorkbook();
        //获得默认sheet
        ISheet primarySheet = workbook.getPrimarySheet();
        //设置根主题的标题
        rootTopic.setTitleText("测试！");
        // 根主题对象
        rootTopic.setStructureClass("org.xmind.ui.logic.left");
        // 创建根节点
        ITopic rootTopic = primarySheet.getRootTopic();
        ITopic topicSon = workbook.createTopic();
        topicSon.setTitleText(mindMapNode.getContentTxt());
        // 备注
        IPlainNotesContent plainContent = (IPlainNotesContent) workbook.createNotesContent(INotes.PLAIN);
        plainContent.setTextContent(mindMapNode.getRemark());
        INotes notes = topicSon.getNotes();
        notes.setContent(INotes.PLAIN, plainContent);
        buildNode(mindMapNode.getSonNode(),topicSon,workbook);
        topic.add(topicSon);
//组装节点到根节点
        rootTopic.add(topicSon);
    }*/
    /**
     * 创建脑图
     * @param nodes 目标节点
     * @param root 根节点名称
     * @param os 输出
     * @throws IOException
     * @throws CoreException
     */
    public static void createMindMap(List<ExportTeamVO> nodes, String root, HttpServletResponse response) throws IOException, CoreException {
        //创建脑图工作空间
        IWorkbookBuilder workbookBuilder = Core.getWorkbookBuilder();
        IWorkbook workbook = workbookBuilder.createWorkbook();
        //获得默认sheet
        ISheet primarySheet = workbook.getPrimarySheet();
        ITopic rootTopic = primarySheet.getRootTopic();
        //根节点的标题
        rootTopic.setTitleText(root);
        rootTopic.setStructureClass("org.xmind.ui.logic.right");
        buildNode(nodes,rootTopic,workbook);
        workbook.save(response.getOutputStream());
    }

    /**
     * 创建脑图节点
     * @param mindMapNodes 节点结合
     * @param topic 根节点
     * @param workbook 工作空间
     */
    private static void buildNode(List<ExportTeamVO> mindMapNodes, ITopic topic, IWorkbook workbook) {
        if (ObjectUtil.isEmpty(mindMapNodes)) {
            return;
        } else {
            for (ExportTeamVO mindMapNode : mindMapNodes) {
                ITopic topicSon = workbook.createTopic();
                topicSon.setTitleText(mindMapNode.getEmpno() + "-" + mindMapNode.getNickName());
                // 备注
                IPlainNotesContent plainContent = (IPlainNotesContent) workbook.createNotesContent(INotes.PLAIN);
//                plainContent.setTextContent(mindMapNode.getEmpno());
                INotes notes = topicSon.getNotes();
                notes.setContent(INotes.PLAIN, plainContent);
                buildNode(mindMapNode.getChild(),topicSon,workbook);
                topic.add(topicSon);
            }
        }
    }



}